﻿using Assets.BattleSystem.EnemyConfigs;
using Assets.Script.BattleSystem.Tools;
using Assets.Script.Data.EnemyData;
using Assets.Script.Human.Enemy;
using LiteNetLib.Utils;
using Newtonsoft.Json;
using SimpleTools.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Script.BattleSystem.Core
{
    public class EnemyManager
    {
        private Dictionary<int, EnemyAI> enemyDic = new Dictionary<int, EnemyAI>();

        private BattleManager battleManager;
        public EnemyManager(BattleManager battleManager,CaseSpawnConfig caseConfig)
        {
            this.battleManager = battleManager;
            //敌人生成部分初始化
            this.caseConfig = caseConfig;
            nextFrame = caseConfig.enemySpawnInfos[0].spawnFrame;
        }

        public void Update()
        {
            nowFrame++;
            AutoDieCheck();
            SpawnEnemy();
        }

        #region 敌人生成部分
        CaseSpawnConfig caseConfig;
        int nowIndex = 0;
        int nextFrame;
        private int nowFrame = 0;
        private void SpawnEnemy()
        {
            while (nowIndex > -1 && nextFrame >= nowFrame)
            {
                foreach (var item in caseConfig.enemySpawnInfos[nowIndex].enemyConfigs)
                {
                    var e = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(caseConfig.enemyConfigDic[item.cId].perfabPath));
                    e.transform.position = new Vector3(item.x, item.y, item.z);
                    var ai = e.GetComponent<EnemyAI>();
                    Debug.Log(item.dieFrame);
                    ai.Init(caseConfig.enemyConfigDic[item.cId], item.netId, item.dieFrame);
                    enemyDic.Add(item.netId, ai);
                }
                nowIndex++;

                //判断结束
                if (nowIndex >= caseConfig.enemySpawnInfos.Length)
                {
                    nowIndex = -1;
                }
                else
                {
                    nextFrame = caseConfig.enemySpawnInfos[nowIndex].spawnFrame;
                }
            }
        }
        #endregion

        public EnemyAI GetEnemy(ref int ID)
        {
            EnemyAI enemy;
            enemyDic.TryGetValue(ID, out enemy);
            return enemy;
        }

        private void RemoveEnemy(int ID)
        {
            if (enemyDic.ContainsKey(ID))
            {
                enemyDic.Remove(ID);
            }
        }

        public void EnemyDie(int id)
        {
            enemyDic[id].Die();
            RemoveEnemy(id);
        }
        private void AutoDieCheck()
        {
            foreach (var VARIABLE in enemyDic.Keys.ToArray())
            {
                if (nowFrame >= enemyDic[VARIABLE].dieFrame)
                {
                    enemyDic[VARIABLE].Die();
                    enemyDic.Remove(VARIABLE);
                }
            }
        }

        /// <summary>
        /// 同步敌人信息,敌人的死亡信息在其他地方同步
        /// </summary>
        /// <param name="reader">应先读取帧数后传入</param>
        public void SyncEnemys(NetDataReader reader)
        {
            int count = reader.GetInt();
            for (int i = 0; i < count; i++)
            {
                EnemyAI ai;
                enemyDic.TryGetValue(reader.GetInt(), out ai);
                if (ai!=null)
                {
                    var data = new BattleSystem.SyncModle.EnemySyncData_Body();
                    data.spaceInfo = new SyncModle.SpaceInfo();
                    data.animIndex = reader.GetInt();
                    data.atkInex = reader.GetInt();
                    data.spaceInfo.pos.x = reader.GetFloat();
                    data.spaceInfo.pos.y = reader.GetFloat();
                    data.spaceInfo.pos.z = reader.GetFloat();
                    ai.UpdateSyncData(data);
                }
            }
        }
    }
}
